package com.nd.mobile.study.view;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.nd.mobile.study.R;
import com.nd.mobile.study.common.App;
import com.nd.mobile.study.common.BaseActivity;
import com.nd.mobile.study.common.constants.BundleConstants;
import com.nd.mobile.study.common.constants.SharedPreferenceConstants;
import com.nd.mobile.study.common.util.L;
import com.nd.mobile.study.common.util.NetworkUtil;
import com.nd.mobile.study.common.util.SharedPreferencesUtil;
import com.nd.mobile.study.common.util.StringUtils;
import com.nd.mobile.study.domain.User;

public class InitActivity extends BaseActivity {
    
    private static  String TAG = "InitActivity";

    //配置文件中的版本号
    private int mSpVersion;

    //系统版本号
    private int mSystemVersion;
    
    //计时跳转，起始时间
    private long   mStartTime;
    /**
     * 闪屏动画时间 0为关闭动画
     */
    public static final int SPLASH_TIME   = 0;
    static final int LOGIN_JUMP = 99991;
    
    @Override
    protected void onBaseCreate(Bundle savedInstanceState) {
        setContentView(R.layout.init_layout);
        mStartTime = System.currentTimeMillis();
    }

    @Override
    protected void onResume() {
        try {
            // 判断是否是最新版本--如果是第一进入最新版本则展示 图片动画
            SharedPreferencesUtil sp = SharedPreferencesUtil.getSyscfgSp(this);
            mSpVersion = sp.getIntValue(SharedPreferenceConstants.SYSCFG_LASTEST_VERSION);
            PackageInfo packageInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            mSystemVersion = packageInfo.versionCode;
            
        } catch (NameNotFoundException e) {
            L.e(TAG, e.getMessage());
        }

        App.screenWidth = getScreenWidth();
        //跳转
        long currentTime = System.currentTimeMillis();
        long remainTime = SPLASH_TIME - (currentTime - mStartTime);
        Message jumpMsg = mainHandler.obtainMessage();
        jumpMsg.what = LOGIN_JUMP;
        if (remainTime > 100) {
            mainHandler.sendMessageDelayed(jumpMsg, remainTime);
        } else {
            mainHandler.sendMessage(jumpMsg);
        }
        super.onResume();
    }
    
    
    private final Handler mainHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            Bundle data = msg.getData();
            switch (msg.what) {
                case LOGIN_JUMP:
                	gotoMain();
                    break;
                }
                	
            }
    };
    
	@Override
	protected void onAfterCreate(Bundle savedInstanceStat) {
	}

    @Override
    protected void initViews() {

    }
    
    private void gotoMain() {
    	String [] userInfos = User.getUserInfoFromSharedPreferences((App)this.getApplication());
    	String username = userInfos[0];
    	String password = userInfos[1];
    	if(StringUtils.isBlank(username) == false){
    		// 执行登录
    		Intent intent = new Intent();
    		intent.setClass(this, LoginLoadingActivity.class);
    		intent.putExtra(BundleConstants.LOGIN_USERNAME, username);
    		intent.putExtra(BundleConstants.LOGIN_PASSWORD, password);
    		startActivity(intent);
    	}else{
            Intent intent = new Intent();
            intent.setClass(InitActivity.this, TabMainActivity.class);
            startActivity(intent);
    	}
    }
}
